
export default {
	common: {
		baseUrl: "",
		data: {},
		header: {
			"Content-Type": "application/json",
			"Content-Type": "application/x-www-form-urlencoded"
		},
		method: 'GET',
		dataType: 'json'
	},
	request(options = {}){
		options.url = this.common.baseUrl + options.url;
		options.data = this.common.data || this.common.data;
		options.header = options.header || this.common.header
		options.method = options.method || this.common.method
		options.dataType = options.dataType || this.common.dataType
		return new Promise((resolve, reject) => {
			uni.request({
				...options,
				success: (res) => {
					if(res.status != 200){
						reject(res)
					}
					resolve(res)
				},
				fail: (err) => {
					reject(err)
				}
			})
		})
	}
}

let $request = (url, data = {}, method = 'GET', options) => {
	return new Promise((resolve, reject) => {
		if(options.loading){
			uni.showLoading({
				title: '数据加载中'
			})
		}
		uni.request({
			url,
			method,
			data,
			success: res => {
				resolve(res)
			},
			fail: error => {
				reject(error)
			},
			complete(){
				uni.hideLoading()
			}
		})
	})
}

let $get = (url, data) => {
	return $request(url, data, "GET")
}

let $post = (url, data) => {
	return $request(url, data, 'POST')
}

uni.$request = $request
uni.$get = $get
uni.$post = $post